import React, {PureComponent} from "react";
import {connect} from "react-redux";
import {Modal, notification, Tabs} from "antd";
import {create, hideCreate, query} from "./actions";
import {getCurrentOrganization, getPrincipal} from "../../lib/identity";
import OrderInfoModal from "./OrderInfoModal";
import {formatDate, formatDateTime} from "../../lib/func";
import OrderOutFeeModal from "./OrderOutFeeModal";

const TabPane = Tabs.TabPane;

class CreateModal extends PureComponent {
    handleCreate = () => {
        const {dispatch, model, fees, outFees, filter} = this.props;
        model.pickDate = formatDate(model.pickDate);
        model.preDepartureTime = formatDateTime(model.preDepartureTime);
        model.actDepartureTime = formatDateTime(model.actDepartureTime);
        dispatch(create({
            ...model,
            "opId": getPrincipal().id,
            "organizationId": getCurrentOrganization(),
            "outFeeList": outFees,
            "feeList": fees
        })).then(action => {
            if (action.error !== true) {
                dispatch(hideCreate());
                dispatch(query({...filter}));
                notification.success({
                    message: '创建成功'
                });
            } else {
                notification.error({
                    message: '创建失败:' + action.message
                });
            }
        });
    };
    handleCancel = () => {
        const {dispatch} = this.props;
        dispatch(hideCreate());
    };

    render() {
        const {loading, visible, model} = this.props;
        return (
            <Modal
                title="新增"
                visible={visible}
                onOk={this.handleCreate}
                onCancel={this.handleCancel}
                confirmLoading={loading}
                okText="保存"
                cancelText="取消"
                width="80%"
            >
                <Tabs defaultActiveKey="1">
                    <TabPane tab="订单信息" key="1">
                        <OrderInfoModal model={model}/>
                    </TabPane>
                    {/*<TabPane tab="应收录入" key="2">
                        <OrderFeeModal model={model}/>
                    </TabPane>*/}
                    <TabPane tab="应付录入" key="3">
                        <OrderOutFeeModal model={model}/>
                    </TabPane>
                </Tabs>
            </Modal>

        );
    }
}

const mapStateToProps = state => {
    return {
        ...state.ghOrderPayable.create,
        clientList: state.ghOrderPayable.list.clientList,
        fees: state.ghOrderPayable.orderFee.fees,
        outFees: state.ghOrderPayable.orderOutFee.fees,
        carrierList: state.ghOrderPayable.list.carrierList,
        filter: state.ghOrderPayable.list.filter
    };
};

export default connect(mapStateToProps)(CreateModal);
